package chuangyi.com.org.ViewModel;

import android.content.Context;
import android.databinding.Bindable;

import java.util.List;

import chuangyi.com.org.DOMIHome.NotifyListChanged;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;
import me.maxwin.view.XListView;

/**
 * Created by zxh on 16/12/1.
 */

public class XListModel extends ObservableWithGoBackHandler
        implements XListView.IXListViewListener, NotifyListChanged {
    private boolean loading;
    private int currentPage;
    private XListAdapterInterface adapter;

    @Bindable
    public XListAdapterInterface getAdapter() {
        return adapter;
    }

    protected void setAdapter(XListAdapterInterface adapter) {
        this.adapter = adapter;
        if (this.adapter != null) {
            this.adapter.setxListViewListener(this);
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    protected void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public XListModel(Context context) {
        super(context);
        adapter = new XListBindingAdapter(getContext(), 0);
        adapter.setxListViewListener(this);
    }

    @Override
    public void onRefresh() {
        if (loading) {
            return;
        }
        loading = true;
        currentPage = 1;
        if (this.adapter != null) {
            this.adapter.clear(true);
            this.adapter.getxListView().setPullLoadEnable(true);
        }
        load(currentPage);
    }

    @Override
    public void onLoadMore() {
        if (loading) {
            return;
        }
        loading = true;
        load(currentPage + 1);
    }

    public void load(final int page) {
    }

    public void stopLoad() {
        if (adapter != null && adapter.getxListView() != null) {
            adapter.getxListView().stopRefresh();
            adapter.getxListView().stopLoadMore();
        }
        loading = false;
    }

    public void setNoMore() {
        if (adapter != null && adapter.getxListView() != null) {
            this.adapter.getxListView().setPullLoadEnable(false);
        }
    }

    public void setHaveMore() {
        if (adapter != null && adapter.getxListView() != null) {
            this.adapter.getxListView().setPullLoadEnable(true);
        }
    }

    @Override
    public void notifyListChanged(final int action, final BindingListItem item) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                switch (action) {
                    case ListBindingAdapter.ACTION_DEL:
                        adapter.remove(item);
                        break;
                    case ListBindingAdapter.ACTION_ADD:
                        adapter.add(item);
                        break;
                }
            }
        });

        notifyDataSetChanged(adapter);
    }

    public void notifyDataSetChanged(final List result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopLoad();
                if (result.size() == 0) {
                    setNoMore();
                }
                adapter.notifyDataSetChanged();
            }
        });
    }
}
